package com.playground.client.websockets.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import com.playground.client.websockets.application.ChatSampleApplication;
import com.playground.client.websockets.application.GameSelectionApplication;
import com.playground.client.websockets.application.LoginApplication;
import com.playground.client.websockets.application.QueueObserver;
import com.playground.client.websockets.application.boardgame.CheckersApplication;
import com.playground.client.websockets.application.boardgame.TicTacToeApplication;
import com.playground.core.repository.GameTitleRepository;
import com.playground.core.repository.RoomRepository;
import com.playground.core.service.LoginService;
import com.playground.core.service.QueueService;
import com.playground.core.service.RoomService;
import com.playground.core.service.factory.GameFactory;
import com.playground.core.service.ruler.CheckersRuler;
import com.playground.core.service.ruler.TicTacToeRuler;

import com.sun.grizzly.websockets.WebSocketEngine;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class WebSocketServlet extends HttpServlet {
    private ApplicationContext springContext;
    
    // services and repositories   
    private LoginService loginService;
    private GameTitleRepository gameTitleRepository;
    private QueueService queueService;
    private RoomService roomService;
    private RoomRepository roomRepository;
    private GameFactory gameFactory;
    private CheckersRuler checkersRuler;
    private TicTacToeRuler ticTacToeRuler;
    
    // applications
    private ChatSampleApplication myApp = new ChatSampleApplication();
    private LoginApplication loginApplication;
    private GameSelectionApplication gameSelectionApplication;
    private CheckersApplication checkersApplication;
    private TicTacToeApplication ticTacToeApplication;
    
    // observers
    private QueueObserver queueObserver;
    
    //private GsonBuilder gsonBuilder;

    private static final long serialVersionUID = 1L;

    @Override
    public void init(ServletConfig config) throws ServletException {
        springContext = new ClassPathXmlApplicationContext("jndi:/server/playground-websockets/WEB-INF/beans.xml");
        
        // get references to services and repositories
        
        loginService = (LoginService) springContext.getBean("loginService");
        gameTitleRepository = (GameTitleRepository) springContext.getBean("gameTitleRepository");
        queueService = (QueueService) springContext.getBean("queueService");
        roomService = (RoomService) springContext.getBean("roomService");
        roomRepository = (RoomRepository) springContext.getBean("roomRepository");
        gameFactory = (GameFactory) springContext.getBean("gameFactory");
        checkersRuler = (CheckersRuler) springContext.getBean("checkersRuler");
        ticTacToeRuler = (TicTacToeRuler)springContext.getBean("ticTacToeRuler");
        
        // initialize observers and link them to their subjects
        
        queueObserver = QueueObserver.getInstance();
        queueObserver.setQueueService(queueService);
        queueService.addObserver(queueObserver);
        
        // initialize applications
        
        loginApplication = new LoginApplication(loginService);
        gameSelectionApplication = new GameSelectionApplication(loginService, gameTitleRepository, queueService, roomService);
        checkersApplication = new CheckersApplication(loginService, roomRepository, gameFactory, checkersRuler);
        ticTacToeApplication = new TicTacToeApplication(loginService, roomRepository, gameFactory, ticTacToeRuler);
        // register applications
        
        WebSocketEngine.getEngine().register(myApp);
        if (null != loginApplication){
            WebSocketEngine.getEngine().register(loginApplication);
            System.out.println("***LoginApplication registered successfully in the Servlet***");
            WebSocketEngine.getEngine().register(gameSelectionApplication);
            System.out.println("***GameSelectionApplication registered successfully in the Servlet***");
            WebSocketEngine.getEngine().register(checkersApplication);
            System.out.println("***CheckersApplication registered successfully in the Servlet***");
            WebSocketEngine.getEngine().register(ticTacToeApplication);
            System.out.println("***TicTacToeApplication registered successfully in the Servlet***");
        } else{
            System.out.println("!!!LoginApplication failed to be registered!!!");
            System.out.println("!!!GameSelectionApplication failed to be registered!!!");
            System.out.println("!!!CheckersApplication failed to be registered!!!");
        }
    }
}
